I'm trying to setup a server with nginx and docker-compose, but I get these errors every time I try 'docker-compose up':
webserver | 2019/06/10 13:04:16 [emerg] 1#1: "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
webserver | nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
I've tried wrapping all up with html {}, removing server {}, another port instead of 80...
nginx Dockerfile
FROM nginx
COPY default.conf /etc/nginx/conf.d/default.conf
default.conf
server {
listen 80;
server_name localhost;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://app:8080/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
This happens when you are trying to overwrite the default nginx config file which does not accept some root properties like http, user. If you need those extra configurations you can try copying what you have to /etc/nginx/nginx.conf
So instead of this: COPY default.conf /etc/nginx/conf.d/default.conf
Do this: COPY nginx.conf /etc/nginx/nginx.conf
Note: By copying to the
/etc/nginx/conf.d/, you will be overwriting the default config.
I would like to add another solution to the ones proposed above. As above suggested, substituting nginx.conf or default.conf works. I do it in docker-compose.yml:
volumes:
- ./services/nginx.conf:/etc/nginx/nginx.conf
However, I came across the necessity to use environment variables in my nginx.conf file, which requires creating a template in /etc/nginx/templates/, which envsubst then outputs in /etc/nginx/conf.d, like explained here.
I came across the following errors:
nginx: [emerg] "events" directive is not allowed here in /etc/nginx/conf.d/default.confnginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/default.confHere is the working solution for the nginx.conf file (the file name does not matter here, since it is changed when copied to container, see docker-compose below, but I like to name it nginx.conf for clarity):
server {
listen ${FE_PORT};
location / {
proxy_pass http://frontend:${FE_PORT};
}
}
server {
listen ${BE_PORT};
location / {
proxy_pass http://backend:${BE_PORT};
}
}
server {
listen ${SERVICE_PORT};
location / {
proxy_pass http://service:${SERVICE_PORT};
}
}
And docker-compose.yml:
nginx:
image: nginx
container_name: nginx
ports:
- "${FE_PORT}:${FE_PORT}"
- "${BE_PORT}:${BE_PORT}"
- "${SERVICE_PORT}:${SERVICE_PORT}"
environment:
- FE_PORT
- BE_PORT
- SERVICE_PORT
volumes:
- ./nginx.conf:/etc/nginx/templates/default.conf.template
Solved this by overwrting nginx.conf.
Dockerfile
FROM nginx
COPY default.conf /etc/nginx/conf.d/default.conf
default.conf
worker_processes 1;
events { worker_connections 1024; }
http {
sendfile on;
upstream app {
server app:8080;
}
server {
listen 8080;
location / {
proxy_pass http://app;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}